/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package pl.gda.pg;

import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import java.net.UnknownHostException;
import java.util.List;

/**
 *
 * @author M. Zawadzki
 */
public class MongoBook {

    /**
     * @param args the command line arguments
     * @throws UnknownHostException
     */
    public static void main(String[] args) throws UnknownHostException {
        System.out.println("*** Welcome to MongoBook - the best phonebook ever! ***\n");

        MongoBook abook = new MongoBook();
        abook.start();
    }
    
    public MongoBook() throws UnknownHostException {
        DBUtils.connect();
    }

    public void start() {
        
        boolean isEnd = false;
        
        while(!isEnd) {
            int option = TextUtils.showMenu("What do you want to do?", "Show all", "Insert",
                    "Search by name", "Search by city", "Delete", "Update");

            switch (option) {
            case 1:
                System.out.println("All contacts:");
                showEntries(DBUtils.getAllEntries());
                break;
            case 2:
                System.out.println("Inserting");
                String imie = TextUtils.readLine("Podaj imie");
                while (DBUtils.createEntry(imie) == -1);
                break;
            case 3:
                String name = TextUtils.readLine("Podaj imie");
                System.out.println("Kontakty z imieniem " + name);

                showEntries(DBUtils.getEntriesByName(name));
                break;
            case 4:
                String city = TextUtils.readLine("Podaj miasto");
                System.out.println("Kontakty z miastem " + city);

                showEntries(DBUtils.getEntriesByCity(city));
                break;
            case 5:
                System.out.println("Deleting");
                String imied = TextUtils.readLine("Podaj imie");
                DBUtils.deleteEntry(imied);
                break;
            case 6:
                System.out.println("Updating");
                DBUtils.updateEntry();
                break;
            case 0:
                isEnd = true;
                break;
            }
        }
    }
    
    public void showEntries(List<DBObject> entries) {
        int i = 1;
        
        for (DBObject entry : entries) {
            System.out.println(String.valueOf(i) + ".");
            System.out.println("Imie: " + entry.get("imie"));
            System.out.println("Nazwisko: " + entry.get("nazwisko"));
            
            BasicDBObject phoneNumbers = (BasicDBObject)entry.get("telefony");
            
            System.out.println("Telefony:");
            System.out.println("\t sluzbowy: " + phoneNumbers.get("sluzbowy"));
            System.out.println("\t domowy: " + phoneNumbers.get("domowy"));
            
            BasicDBObject mailAddresses = (BasicDBObject)entry.get("maile");
            
            System.out.println("Maile:");
            System.out.println("\t sluzbowy: " + mailAddresses.get("sluzbowy"));
            System.out.println("\t domowy: " + mailAddresses.get("domowy"));
            
            BasicDBObject addresses = (BasicDBObject)entry.get("adresy");
            BasicDBObject workAddress = (BasicDBObject)addresses.get("firmowy");
            
            System.out.println("Adres sluzbowy:");
            System.out.println("\t ulica: " + workAddress.get("ulica"));
            System.out.println("\t nr domu: " + workAddress.get("nrdomu"));
            System.out.println("\t nr mieszkania: " + workAddress.get("nrmieszkania"));
            System.out.println("\t kod: " + workAddress.get("kod"));
            System.out.println("\t miasto: " + workAddress.get("miasto"));
            
            boolean maZwierze = (boolean)entry.get("zwierz");
            String maZwierzeText = maZwierze ? "Tak" : "Nie";
            
            System.out.println("Ma zwierze: " + maZwierzeText);
            
            i++;
        }
    }
}
